<?php
namespace Common\Common;
use Model\RedisModel;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Yujie
 * Date: 2018/9/3
 * Time: 16:43
 */
class BaseController extends Controller{
    protected $_now_time;
    protected $_redis;
    public function __construct()
    {
        parent::__construct();
        $this->assign('title','码猿轩');
        $this->_now_time = time();
        $this->_redis = RedisModel::getDb()->getConnect();
        $this->recoreClient();
    }

    private function recoreClient(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"]))
        {
            $cip = $_SERVER["HTTP_CLIENT_IP"];
        }
        else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
        {
            $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        else if(!empty($_SERVER["REMOTE_ADDR"]))
        {
            $cip = $_SERVER["REMOTE_ADDR"];
        }
        else
        {
            $cip = '';
        }

        $this->_redis->sAdd('access_ip_log_'.date('Ymd',$this->_now_time),$cip);
        $this->_redis->hIncrBy('access_ip_num_'.date('Ymd',$this->_now_time),$cip,1);

    }
    public function successView($detail = array(), $msg = '', $ret_arr = true, $pin_id = 0, $badge = '')
    {
        if ($ret_arr == true) {
            $detail = (array)$detail;
        } else {
            $detail = (object)$detail;
        }
        $info['rs_code'] = 1000;
        $info['rs_msg'] = $msg;
        $info['details'] = $detail;

        $info = json_encode($info);
        $info = str_replace("null", '""', $info);
        exit($info);
    }

    /*
     * 返回错误信息   并做日志记录  （在并发较高时，应关掉日志读写）
     */

    public function errorView($rc, $error_message, $detail = array())
    {
        ob_clean();
        $rc = intval($rc);
        $info = array(
            'rs_code' => $rc,
            'rs_msg' => $error_message);
        $info['details'] = (array)$detail;

        $info = json_encode($info);

        exit($info);
    }
}